import {
  createWebHistory,
  createRouter,
  RouteRecordNameGeneric,
} from "vue-router";
import { routes } from "./route";

const routePath = <any>[];
routes.map((item) => {
  item.children.map((ele) => {
    routePath.push(ele);
  });
});

const routesNameSet = new Set<RouteRecordNameGeneric>(
  routePath.map((item: any) => item.name)
);

const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach(async (to) => {
  if (!routesNameSet.has(to.name)) {
    return { name: "404" };
  }
});

export default router;
